//
//  BaseViewController.m
//  XinYiGou
//
//  Created by Angel_Yan on 2017/3/15.
//  Copyright © 2017年 Angel_Yan. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
{
    UIView *_noDataView;
    UILabel *_labLine;
    blockVC vBlock;
}
@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    self.view.userInteractionEnabled = YES;
    if (self.isPopGuestureEnable == NO) {
        kWeakSelf(self)
        self.navigationController.interactivePopGestureRecognizer.delegate = weakself;
        if (self.navigationController && self.navigationController.viewControllers.count != 1) {
            self.navigationController.interactivePopGestureRecognizer.enabled = self.isPopGuestureEnable;
        }
    }
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isPopGuestureEnable = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupData];
    [self setup];
    [self requestData];
    [self setupConfig];
}


/**
 *  设置一些默认值,通知，数组等的init
 */
- (void)setupData {
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
            case AFNetworkReachabilityStatusNotReachable:
                [self networkReachablityChanged:NO];
                break;
            default:
                [self networkReachablityChanged:YES];
                break;
        }
    }];
}


/**
 * 统一UI界面搭建处理方法
 */
- (void)setup {
    
}

/**
 * 说明 统一的请求始化数据方法
 */
- (void) requestData
{
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
    [self hideKeyboard];
    kWeakSelf(self)
    self.navigationController.interactivePopGestureRecognizer.delegate = weakself;
    if (self.navigationController && self.navigationController.viewControllers.count != 1) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//	[CustomMBHUD hide];
}


- (void)cleanSelf {
	NSLog(@"[%@] dealloc......", NSStringFromClass(self.class));
	removeNOTIbserverAll;
}

-(UITableView *)setTableView:(CGRect)frame {
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView setShowsVerticalScrollIndicator:NO];
    return tableView;
}

- (void)dealloc {
	[self cleanSelf];
}

- (UIScrollView *)scrViewContent {
    if (_scrViewContent == nil) {
//        _scrViewContent = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight)];
        _scrViewContent = [[UIScrollView alloc] initWithFrame:CGRectZero];
//        _scrViewContent.contentSize = CGSizeMake(_scrViewContent.width, _scrViewContent.height);
//        _scrViewContent.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_scrViewContent];
        
        [_scrViewContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(SafeAreaTopHeight, 0, 0, 0));
        }];
        
        AdjustsScrollViewInsetNever(self, self.scrViewContent);

    }
    return _scrViewContent;
}


- (void)setNavTitle:(NSString *)navTitle {
    _navTitle = navTitle;
    if (_navTitle != nil) {
        [self.navigationItem setNewTitle:_navTitle];
    }
}




- (void)setupConfig{
    [self.view setBackgroundColor:[UIColor colorWithARGBString:@"#f5f5f5"]];

    if(self.isPresentBreak){
        [self.navigationItem setItemWithTarget:self isLeft:YES action:@selector(pullBack) title:@"关闭" imageNor:nil];
    }else {
        if(self.navigationController && self != [self.navigationController.viewControllers firstObject]){
            if(self.navigationController.viewControllers.count>6)
                [self.navigationItem setWebLeftItemWithTarget:self action:@selector(pullBack) action:@selector(pullTop)];
            else
                [self.navigationItem setBackItemWithTarget:self action:@selector(pullBack) title:@""];
        }

        self.navigationController.interactivePopGestureRecognizer.delegate = (AFFNavigationController*)self.navigationController; // 控制返回手势的
    }

    if(self.navigationItem.titleView && ![NSString isNilOrEmpty:self.navigationItem.title]){
        [self.navigationItem setNewTitleConfig];
    }
}

- (void)pullBack{
    if(self.isPresentBreak || self.navigationController==nil /* || (self.navigationController.viewControllers.count==1&&self.tabBarController&&![self.tabBarController isEqual:kAppDelegate.vcMain]) */){
        [self dismissViewControllerAnimated:YES completion:^{
            [self cleanSelf];
        }];
    }else {
        [self cleanSelf];
        UIViewController *search;
        for (UIViewController *temp in self.navigationController.viewControllers) {
          
        }
        if (search) {
            NSInteger index = [self.navigationController.viewControllers indexOfObject:search];
            if (index > 1) {
                UIViewController *vc = self.navigationController.viewControllers[index-1];
                [self.navigationController popToViewController:vc animated:YES];
            } else {
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (blockVC)vcBlock {
    return vBlock;
}

- (void)actionResult:(blockVC)block {
    vBlock = block;
}

- (void)setIsPopGuestureEnable:(BOOL)isPopGuestureEnable {
    _isPopGuestureEnable = isPopGuestureEnable;
}


#pragma mark - 私有方法


- (void)singleTapped:(UIGestureRecognizer *)gestureRecognizer {
    [self performSelector:@selector(hideKeyboard) withObject:nil afterDelay:0.1f];
}



- (void)hideKeyboard {
    [self.view endEditing:YES];
}



#pragma mark - 网络状态改变的通知

- (void)notoworkChanged:(NSNotification *)notiInfo {
    AFNetworkReachabilityStatus status = [notiInfo.userInfo[AFNetworkingReachabilityNotificationStatusItem] intValue];
    if (status == AFNetworkReachabilityStatusNotReachable) {
        [self networkReachablityChanged:NO];
    } else if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
        [self networkReachablityChanged:YES];
    }
}



- (void)networkReachablityChanged:(BOOL)reachable {
    if (!reachable) {
        // 无网络
        //[self showResultThenHide:@"网络异常了，请检查网络"];
    } else {
        [self ishavingNetworkForRefresh];
        _noDataView.hidden = YES;
    }
}

/**
 *  网络恢复
 */
- (void)ishavingNetworkForRefresh
{
    
    
}


/// 旋转
- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationPortrait;
}



@end

@implementation UIViewController  (Anyfish)

- (AFFNavigationController *)aNavigationController{
    return (AFFNavigationController*)self.navigationController;
}

@end

@implementation BaseCellModel
@end
